package com.education.welco.controller;


import com.education.common.core.BaseController;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.FileGetSizeUtil;
import com.education.welco.feign.InfoMsgFeign;
import com.education.welco.pojo.*;
import com.education.welco.service.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 学校课程资源 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@RequestMapping("/newsResources")
public class SResourcesNewController extends BaseController {

    @Autowired
    private ISResourcesService resourcesService;
    @Autowired
    private ISCatalogResourceSynService resourceSynService;
    @Autowired
    private ISCatalogMenuService catalogMenuService;
    @Autowired
    private ISCatalogClassService catalogClassService;
    @Autowired
    private ITClassService classService;
    @Autowired
    private ISCatalogService catalogService;
    @Autowired
    private ISResourceClassService resourceClassService;
    @Autowired
    private ITSchooldeptService schooldeptService;
    @Autowired
    private ISResClassService resClassService;
    @Autowired
    private ISCatalogResourceSynService catalogResourceSynService;
    @Autowired
    private InfoMsgFeign msgFeign;


    /**
     * 获取到最上面一级的id     查看班级列表 课程分享班级
     */
    public Integer GetFirstId2(Integer catalogId) {
        SCatalogMenu sCatalogMenu = catalogMenuService.selectSCatalogMenuById(catalogId);
        //第二级
        if (sCatalogMenu.getParentId() != 0) {
            //第一级
            SCatalogMenu sCatalogMenu1 = catalogMenuService.selectSCatalogMenuById(sCatalogMenu.getParentId());
            return GetFirstId2(sCatalogMenu1.getId());
        }
        return sCatalogMenu.getId();
    }


    /**
     * 查询资源查看班级    右侧列表
     */
    /*@GetMapping("/resClassList")
    public ApiReturnObject resourceClass(Integer catalogId, String type) {
        TClass tClass1 = new TClass();
        tClass1.setCatalogId(catalogId);
        tClass1.setResourceType(type);
        List<TClass> classes = classService.selectTClassByCatalogIdAndType(tClass1);
        if (classes.size() > 0) {
            for (TClass tClass : classes) {
                TSchooldept schooldept = schooldeptService.selectTSchooldeptByClassId(tClass.getDeptId());
                //选择在授班级时上级部门查询错误修改
                if (StringUtils.isNotNull(schooldept)) {
                    tClass.setDeptId(schooldept.getId());
                    tClass.setDeptName(schooldept.getDeptName());
                }
            }
            return new ApiReturnObject("200", "查询成功", classes);
        }
        return new ApiReturnObject("200", "结果为空", classes);
    }*/

    /*
     *   修改资源查看班级
     */
    /*@PostMapping("/updateClass")
    public ApiReturnObject updateClass(@RequestBody SResources resources) {
        //catalogId     type        classList
        if (resources.getCatalogId() != null && resources.getType() != null && !"".equals(resources.getType())) {
            SResClass resClass = new SResClass();
            resClass.setCatalogId(resources.getCatalogId());
            resClass.setResType(resources.getType());
            //先删除中间表数据
            resClassService.deleteSCatalogResourceSynByCatalogIdAndType(resClass);
            if (resources.getClassList() != null && resources.getClassList().size() > 0) {
                resClass.setCreateTime(new Date());
                for (TClass tClass : resources.getClassList()) {
                    resClass.setClassId(tClass.getId());
                    resClassService.insertSCatalogResourceSyn(resClass);
                }
            }

            //往另一张中间表添加数据
            if (resources.getContentList().size() > 0) {
                //删除中间表数据
                for (SResources sResources : resources.getContentList()) {
                    resourceClassService.deleteSResourceClassByresourceId(sResources.getId());

                    SResourceClass resourceClass = new SResourceClass();
                    resourceClass.setResourceId(sResources.getId());
                    resourceClass.setCreateTime(new Date());
                    for (TClass tClass : resources.getClassList()) {
                        resourceClass.setClassId(tClass.getId());
                        resourceClassService.insertSResourceClass(resourceClass);
                    }
                }
            }

            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }*/


    /**
     * 查询资源查看内容
     */
    @GetMapping("/resContentList")
    public ApiReturnObject resourceContent(Integer catalogId, String type) {
        //所有该类别内容
        SResources resources = new SResources();
        resources.setRCatalogid(catalogId);
        resources.setType(type);
        List<SResources> sResourceList = resourcesService.selectSResourcesList(resources);

        //格式化创建时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (SResources sResource : sResourceList) {
            //对资源名称做操作
            if (sResource.getRUrl() != null && !"".equals(sResource.getRUrl())) {
                String[] split = sResource.getRUrl().split("\\.");
                String s = split[split.length - 1];
                sResource.setRName(sResource.getRName() + "." + s);
            }
            //获取文件大小  资源前缀 http://jinruanresource.shixuncloud.com/
            if ("".equals(sResource.getRSize()) || null == sResource.getRSize()) {
                //资源大小
                String qiNiuUrl2 = FileGetSizeUtil.getQiNiuUrl2(sResource.getRUrl());
                sResource.setRSize(qiNiuUrl2);
            }
            //格式化时间
            String format = simpleDateFormat.format(sResource.getCreateTime());
            sResource.setCreateTimeString(format);
        }

        SCatalogResourceSyn resourceSyn = new SCatalogResourceSyn();
        resourceSyn.setCatalogId(catalogId);
        resourceSyn.setResourceType(type);
        // 查看内容 资源列表
        List<SCatalogResourceSyn> sResources1 = resourceSynService.selectSCatalogResourceSynList(resourceSyn);
        if (sResources1 != null && sResources1.size() > 0) {
            if (sResourceList != null && sResourceList.size() > 0) {
                //选中的
                for (SCatalogResourceSyn sCatalogResourceSyn : sResources1) {
                    for (SResources sResources : sResourceList) {
                        //是选中的那一条数据
                        if (sCatalogResourceSyn.getResourceId().equals(sResources.getId())) {
                            sResources.setChecked(true);
                        }
                    }
                }
            }
        }
        return new ApiReturnObject("200", "查询成功", sResourceList);
    }

    /*
     *   修改查看内容
     */
    @PostMapping("/updateContent")
    public ApiReturnObject updateContent(@RequestBody SResources resources) {
        //catalogId     type        classList
        if (resources.getCatalogId() != null && resources.getType() != null && !"".equals(resources.getType())) {
            SCatalogResourceSyn resourceSyn = new SCatalogResourceSyn();
            resourceSyn.setCatalogId(resources.getCatalogId());
            resourceSyn.setResourceType(resources.getType());
            List<SCatalogResourceSyn> sCatalogResourceSyns = catalogResourceSynService.selectSCatalogResourceSynList(resourceSyn);
            //如果中间表中有数据
            if (sCatalogResourceSyns != null && sCatalogResourceSyns.size() > 0) {
                // 改变往中间添加的数据的状态
                SResources resources1 = resourcesService.selectSResourcesById(sCatalogResourceSyns.get(0).getResourceId());
                //根据中间表id 获取数据
                if (resources1 != null) {
                    //修改被选中内容的状态
                    if (resources.getContentIdList() != null && resources.getContentIdList().size() > 0) {
                        //新建一个集合
                        ArrayList<Integer> list = new ArrayList<>();
                        //查询出资源集合
                        List<SResources> sResources = resourcesService.selectSResourcesByIds(resources.getContentIdList());
//                    }
//                    if(resources.getContentList() != null && resources.getContentList().size() > 0){
                        for (SResources resources2 : sResources) {
                            //修改状态
                            resources2.setIsStu(resources1.getIsStu());
                            resources2.setSynch(resources1.getSynch());
                            //进行修改
                            resourcesService.updateSResources(resources2);
                            //查看内容的id
                            list.add(resources2.getId());

                            //修改班级资源中间表数据
                            resourceClassService.deleteSResourceClassByresourceId(resources2.getId());
                            SResourceClass resourceClass = new SResourceClass();
                            resourceClass.setResourceId(resources2.getId());
                            resourceClass.setCreateTime(new Date());
                            for (TClass tClass : resources.getClassList()) {
                                resourceClass.setClassId(tClass.getId());
                                resourceClassService.insertSResourceClass(resourceClass);
                            }

                        }
                        //没有选择了查看内容 将状态重置为 0 0
                        SResources catalogPreview = new SResources();
                        catalogPreview.setCatalogId(resources.getCatalogId());
                        catalogPreview.setType(resources.getType());
                        //该目录全部预习
                        List<SResources> previews = resourcesService.selectSResourcesList(catalogPreview);
                        for (SResources sCatalogPreview : previews) {
                            //如果查看内容里不包括当前内容 修改其状态
                            if (!list.contains(sCatalogPreview.getId())) {
                                SResources preview2 = resourcesService.selectSResourcesById(sCatalogPreview.getId());
                                preview2.setSynch("0");
                                preview2.setIsStu("0");
                                resourcesService.updateSResources(preview2);
                            }
                        }
                    } else {
                        //没有选择了查看内容 将状态重置为 0 0
                        SResources catalogPreview = new SResources();
                        catalogPreview.setCatalogId(resources.getCatalogId());
                        catalogPreview.setType(resources.getType());
                        List<SResources> previews = resourcesService.selectSResourcesList(catalogPreview);
                        for (SResources sCatalogPreview : previews) {
                            sCatalogPreview.setIsStu("0");
                            sCatalogPreview.setSynch("0");
                            resourcesService.updateSResources(sCatalogPreview);
                        }
                    }
                }
                //删除中间表数据
                catalogResourceSynService.deleteSCatalogResourceSynByTypeAndcatalogId(resourceSyn);
            } else {
                //中间表中没有数据
                //修改被选中内容的状态
               /* if(resources.getContentIdList() != null && resources.getContentIdList().size() > 0  ){
                    //查询出资源集合
                    List<SResources> sResources = resourcesService.selectSResourcesByIds(resources.getContentIdList());
//                    }
//                    if(resources.getContentList() != null && resources.getContentList().size() > 0){
                    for (SResources resources2 : sResources) {
                        //修改状态
                        resources2.setIsStu("0");
                        resources2.setSynch("0");
                        //进行修改
                        resourcesService.updateSResources(resources2);
                    }
                }*/
                //没有选择了查看内容 将状态重置为 0 0
                SResources catalogPreview = new SResources();
                catalogPreview.setCatalogId(resources.getCatalogId());
                catalogPreview.setType(resources.getType());
                List<SResources> previews = resourcesService.selectSResourcesList(catalogPreview);
                for (SResources sCatalogPreview : previews) {
                    sCatalogPreview.setIsStu("0");
                    sCatalogPreview.setSynch("0");
                    resourcesService.updateSResources(sCatalogPreview);
                }
            }
            if (resources.getContentIdList() != null && resources.getContentIdList().size() > 0) {
                //往中间表添加数据
                resourceSyn.setCreateTime(new Date());
                for (Integer id : resources.getContentIdList()) {
                    resourceSyn.setResourceId(id);
                    catalogResourceSynService.insertSCatalogResourceSyn(resourceSyn);
                }
            }
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }


    /*
     *   修改查看内容
     */
    @PostMapping("/updateContentByObj")
    public ApiReturnObject updateContentByObj(@RequestBody SResources resources) {
        //catalogId     type        classList
        if (resources.getCatalogId() != null && resources.getType() != null && !"".equals(resources.getType())) {
            SCatalogResourceSyn resourceSyn = new SCatalogResourceSyn();
            resourceSyn.setCatalogId(resources.getCatalogId());
            resourceSyn.setResourceType(resources.getType());
            List<SCatalogResourceSyn> sCatalogResourceSyns = catalogResourceSynService.selectSCatalogResourceSynList(resourceSyn);
            //如果中间表中有数据
            if (sCatalogResourceSyns != null && sCatalogResourceSyns.size() > 0) {
                // 改变往中间添加的数据的状态
                SResources resources1 = resourcesService.selectSResourcesById(sCatalogResourceSyns.get(0).getResourceId());
                //根据中间表id 获取数据
                if (resources1 != null) {
                    //修改被选中内容的状态
                    if (resources.getMatterList() != null && resources.getMatterList().size() > 0) {
                        //新建一个集合
                        ArrayList<Integer> list = new ArrayList<>();
                        //查询出资源集合
                        List<SResources> sResources = resources.getMatterList();
                        for (SResources resources2 : sResources) {
                            if (resources2.getRName() != null && !"".equals(resources2.getRName())) {
                                String s = resources2.getRName().split("\\.")[0];
                                resources2.setRName(s);
                            }
                            //修改状态
                            resources2.setIsStu(resources1.getIsStu());
                            resources2.setSynch(resources1.getSynch());
                            //进行修改
                            resourcesService.updateSResources(resources2);
                            list.add(resources2.getId());

                            //修改班级资源中间表数据
                            resourceClassService.deleteSResourceClassByresourceId(resources2.getId());
                            SResourceClass resourceClass = new SResourceClass();
                            resourceClass.setResourceId(resources2.getId());
                            resourceClass.setCreateTime(new Date());
                            for (TClass tClass : resources.getClassList()) {
                                resourceClass.setClassId(tClass.getId());
                                resourceClassService.insertSResourceClass(resourceClass);
                            }
                        }
                        //将没有被选择的内容修改状态
                        SResources resources2 = new SResources();
                        resources2.setCatalogId(resources.getCatalogId());
                        resources2.setType(resources.getType());
                        //该目录全部预习
                        List<SResources> previews = resourcesService.selectSResourcesList(resources2);
                        for (SResources sCatalogPreview : previews) {
                            //如果查看内容里不包括当前内容 修改其状态
                            if (!list.contains(sCatalogPreview.getId())) {
                                SResources preview2 = resourcesService.selectSResourcesById(sCatalogPreview.getId());
                                preview2.setSynch("0");
                                preview2.setIsStu("0");
                                resourcesService.updateSResources(preview2);
                            }
                        }
                    } else {
                        //没有选择了查看内容 将状态重置为 0 0
                        SResources catalogPreview = new SResources();
                        catalogPreview.setCatalogId(resources.getCatalogId());
                        catalogPreview.setType(resources.getType());
                        List<SResources> previews = resourcesService.selectSResourcesList(catalogPreview);
                        for (SResources sCatalogPreview : previews) {
                            sCatalogPreview.setIsStu("0");
                            sCatalogPreview.setSynch("0");
                            resourcesService.updateSResources(sCatalogPreview);
                        }
                    }
                }
                //删除中间表数据
                catalogResourceSynService.deleteSCatalogResourceSynByTypeAndcatalogId(resourceSyn);
            } else {
                //中间表中没有数据
                //修改被选中内容的状态
               /* if(resources.getMatterList() != null && resources.getMatterList().size() > 0  ){
                    //查询出资源集合
                    List<SResources> sResources = resources.getMatterList();
                    for (SResources resources2 : sResources) {
                        //修改状态
                        resources2.setIsStu("0");
                        resources2.setSynch("0");
                        //进行修改
                        resourcesService.updateSResources(resources2);
                    }
                }*/
                //没有选择了查看内容 将状态重置为 0 0
                SResources catalogPreview = new SResources();
                catalogPreview.setCatalogId(resources.getCatalogId());
                catalogPreview.setType(resources.getType());
                List<SResources> previews = resourcesService.selectSResourcesList(catalogPreview);
                for (SResources sCatalogPreview : previews) {
                    sCatalogPreview.setIsStu("0");
                    sCatalogPreview.setSynch("0");
                    resourcesService.updateSResources(sCatalogPreview);
                }
            }
            if (resources.getMatterList() != null && resources.getMatterList().size() > 0) {
                //往中间表添加数据
                resourceSyn.setCreateTime(new Date());
                for (SResources resources1 : resources.getMatterList()) {
                    resourceSyn.setResourceId(resources1.getId());
                    catalogResourceSynService.insertSCatalogResourceSyn(resourceSyn);
                }
            }
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }


    /**
     * 查询资源查看班级
     */
    public List<TClass> resourceClass2(Integer resourceId) {
        List<TClass> tClasses = classService.selectTClassByResourceId(resourceId);
        if (tClasses.size() > 0) {
            for (TClass tClass : tClasses) {
//                TSchooldept schooldept = schooldeptService.selectTSchooldeptByClassId(tClass.getId());
                TClass tClass1 = classService.selectTClassById(tClass.getId());
                tClass.setDeptId(tClass1.getId());
                tClass.setDeptName(tClass1.getClassName());
            }
        }
        return tClasses;
    }

    /**
     * 根据课程小节的id   查询出资源  只返回一类型
     *
     * @param catalogId
     * @param type
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/listOther")
    public ApiReturnObject listOther(Integer catalogId, String type, Integer pageSize, Integer pageNum) {
        //根据当前id 查询出列表
        if (catalogId != null && type != null && !"".equals(type)) {
            Map<String, Object> map = new HashMap<>(16);
            SResources resources = new SResources();
            resources.setRCatalogid(catalogId);
            //资源的分类
            resources.setType(type);
            List<SResources> sResourceList = resourcesService.selectSResourcesList(resources);
            //格式化创建时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SResources sResource : sResourceList) {
                //对资源名称做操作
                if (sResource.getRUrl() != null && !"".equals(sResource.getRUrl())) {
                    String[] split = sResource.getRUrl().split("\\.");
                    String s = split[split.length - 1];
                    sResource.setRName(sResource.getRName() + "." + s);
                }
                //获取文件大小  资源前缀 http://jinruanresource.shixuncloud.com/
                if ("".equals(sResource.getRSize()) || null == sResource.getRSize()) {
                    //资源大小
                    String qiNiuUrl2 = FileGetSizeUtil.getQiNiuUrl2(sResource.getRUrl());
                    sResource.setRSize(qiNiuUrl2);
                }
                //格式化时间
                String format = simpleDateFormat.format(sResource.getCreateTime());
                sResource.setCreateTimeString(format);
            }
            map.put("sResourceList", sResourceList);
            return new ApiReturnObject("200", "查询成功", map);
        }
        return new ApiReturnObject("201", "缺少相关参数", "");
    }

    /**
     * 修改此门备课的状态
     */
    public SCatalogMenu changeStatus(SResources resources) {
        //将资源所在的备课状态改变
        Integer rCatalogId = resources.getCatalogId();
        //教师创建课程表
        SCatalogMenu catalogMenu = catalogMenuService.selectSCatalogMenuById(rCatalogId);
        //目录最下面一级
        Integer parentId = catalogMenu.getParentId();
        //备课课程id
        Integer integer = GetFirstId2(parentId);
        SCatalogMenu catalogMenu1 = catalogMenuService.selectSCatalogMenuById(integer);
        return catalogMenu1;
    }


    @GetMapping("selectOne")
    public ApiReturnObject selectOne(Integer id) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1589879627069.jpg");
        strings.add("1589879627230.jpg");
        strings.add("1589879627468.jpg");
        strings.add("1589879627675.jpg");
        strings.add("1589879626481.jpg");
        SResources sResource = resourcesService.selectSResourcesById(id);
        //判断图片的类型
        if (strings.contains(sResource.getResIco())) {
            sResource.setIcoType("1");
        } else {
            sResource.setIcoType("2");
        }
        if ("8".equals(sResource.getType())) {
            if (sResource.getRUrl() != null && !"".equals(sResource.getRUrl())) {
                if (sResource.getRUrl().contains(",")) {
                    //true 切割
                    String[] split = sResource.getRUrl().split(",");
                    if (split.length > 1) {
                        //两个路径都有
                        //上传文件map 3d集合
                        HashMap<String, Object> map = new HashMap<>();
                        if (sResource.getRName() != null) {
                            map.put("name", sResource.getRName());
                        }
                        map.put("url", split[0]);
                        sResource.setFileList(map);

                        //上传文件map flash集合
                        HashMap<String, Object> map2 = new HashMap<>();
                        if (sResource.getRName() != null) {
                            map2.put("name", sResource.getRName());
                        }
                        map2.put("url", split[1]);
                        sResource.setFileList2(map2);
                    } else if (split.length == 1) {
                        // 3d
                        //上传文件map 3d集合
                        HashMap<String, Object> map = new HashMap<>();
                        if (sResource.getRName() != null) {
                            map.put("name", sResource.getRName());
                        }
                        map.put("url", split[0]);
                        sResource.setFileList(map);
                    }
                } else {
                    //flash
                    HashMap<String, Object> map2 = new HashMap<>();
                    if (sResource.getRName() != null) {
                        map2.put("name", sResource.getRName());
                    }
                    if (sResource.getRUrl() != null) {
                        map2.put("url", sResource.getRUrl());
                    }
                    sResource.setFileList2(map2);
                }
            }
        }

        //上传文件map 集合
        HashMap<String, Object> map = new HashMap<>();
        if (sResource.getRName() != null) {
            map.put("name", sResource.getRName());
        }
        if (sResource.getRUrl() != null) {
            map.put("url", sResource.getRUrl());
        }
        sResource.setFileList(map);


        //是否同步
        if (sResource.getSynch() != null) {
            if ("0".equals(sResource.getSynch())) {
                sResource.setSynchBool(false);
            } else if ("1".equals(sResource.getSynch())) {
                sResource.setSynchBool(true);
            }
        }
        //是否允许学生查看
        if (sResource.getIsStu() != null) {
            if ("0".equals(sResource.getIsStu())) {
                sResource.setStuBool(false);
            } else if ("1".equals(sResource.getIsStu())) {
                sResource.setStuBool(true);
            }
        }

        //该资源查看班级
        List<TClass> sResourceClasses = resourceClass2(sResource.getId());
        if (sResourceClasses != null && sResourceClasses.size() > 0) {
            sResource.setClassList(sResourceClasses);
            String s = "";
            int size = sResourceClasses.size();
            //拼接查看班级字符串
            for (int i = 0; i < size; i++) {
                if (i == size - 1) {
                    //最后一个
                    s = s + sResourceClasses.get(i).getClassName();
                } else {
                    s = s + sResourceClasses.get(i).getClassName() + ",";
                }
            }
            sResource.setCheckString(s);
        }
        return new ApiReturnObject("200", "查询成功", sResource);
    }

    public SResources updateStatus(SResources resources) {
        //app 端查看 允许
        String i = "1";
        if (i.equals(resources.getIsStu())) {
            SCatalogMenu catalogMenu = changeStatus(resources);
            if (!i.equals(catalogMenu.getIsStu())) {
                //修改备课是否允许学生查看状态
                catalogMenu.setIsStu("1");
                catalogMenuService.updateSCatalogMenu(catalogMenu);
            }
        }
        // 同步到上课资料
        if (i.equals(resources.getSynch())) {
            //同步上课资料
            SCatalogMenu catalogMenu = changeStatus(resources);
            if (!i.equals(catalogMenu.getIsSyn())) {
                //修改同步上课资料状态
                catalogMenu.setIsSyn("1");
                catalogMenuService.updateSCatalogMenu(catalogMenu);
            }
        }
        return resources;
    }


    /**
     * 老师备课模块添加资源
     * 删除-添加中间表中允许查看的班级
     *
     * @param resources
     * @return
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody SResources resources) {
        if (resources.getCatalogId() != null) {
            // 上传资源并 获取文件路径
            if (resources.getRName() != null && !"".equals(resources.getRName())) {
                String s = resources.getRName().split("\\.")[0];
                resources.setRName(s);
            }
            if ("4".equals(resources.getType())) {
                resources.setResIco(resources.getRUrl());
            }
            //获取文件大小
            String size = FileGetSizeUtil.getQiNiuUrl2(resources.getRUrl());
            resources.setRSize(size);
            resources.setRCatalogid(resources.getCatalogId());
            resources.setCreateTime(new Date());
            resourcesService.insertSResources(resources);
            return new ApiReturnObject("200", "添加成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }


    /**
     * 修改课程
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody SResources resources) {
//        if(resources.getId() != null){
        //名称操作
        if (resources.getRName() != null && !"".equals(resources.getRName())) {
            if (resources.getRName().contains(".")) {
                String s = resources.getRName().split("\\.")[0];
                if (s != null) {
                    resources.setRName(s);
                }
            }
        }
        //修改 查看内容 状态  修改上级
        if (resources.getCatalogId() != null && resources.getIsStu() != null && resources.getSynch() != null) {
            updateStatus(resources);
        }
        if (resources.getContentList() != null && resources.getContentList().size() > 0) {
            for (SResources sResources : resources.getContentList()) {
                if (resources.getIsStu() != null && !"".equals(resources.getIsStu())) {
                    sResources.setIsStu(resources.getIsStu());
                }
                if (resources.getSynch() != null && !"".equals(resources.getSynch())) {
                    sResources.setSynch(resources.getSynch());
                }
                sResources.setRRemark("修改");
                resourcesService.updateSResources(sResources);
//                    return new ApiReturnObject("200","修改成功","");
            }
            return new ApiReturnObject("200", "修改成功", "");
        } else {
            return new ApiReturnObject("201", "请选择查看内容", "");
        }
    }

    /**
     * 删除资源 课程资源的实战照片
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ApiReturnObject remove(Integer id) {
        if (id != null) {
//  TODO 教师上传资源一级平台导入资源 删除逻辑优化
            /*SResources resources = resourcesService.selectSResourcesById(id);
            if (StringUtils.isNotBlank(resources.getRUrl())) {
                try {
                    QiniuUtil.deleteFileFromQiniu(resources.getRUrl());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }*/
            resourcesService.deleteSResourcesById(id);
            return new ApiReturnObject("200", "删除成功", "");
        }
        return new ApiReturnObject("201", "缺少相关参数", "");
    }


    public static final Logger LOGGER = LogManager.getLogger(SResourcesNewController.class);


    /**
     * 下载资源
     */
    @GetMapping("/downLoadVideo")
    private void downLoad(String fileUrl, HttpServletResponse response) throws IOException {
        //文件名和路径前面已经传来了，所以不需要了
        //1.获取要下载的文件的绝对路径
        //String realPath = this.getServletContext().getRealPath("/download/1.JPG");
        //2.获取要下载的文件名
        URL url = new URL("http://jinruanresource.shixuncloud.com/" + fileUrl);
        //3.设置content-disposition响应头控制浏览器以下载的形式打开文件
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileUrl, "UTF-8"));
        //4.获取要下载的文件输入流
        //2.获取链接
        URLConnection conn = url.openConnection();
        //3.输入流
        InputStream inStream = conn.getInputStream();
        int len = 0;
        //5.创建数据缓冲区
        byte[] buffer = new byte[1024];
        //6.通过response对象获取OutputStream流
        OutputStream out = response.getOutputStream();
        //7.将FileInputStream流写入到buffer缓冲区
        while ((len = inStream.read(buffer)) > 0) {
            //8.使用OutputStream将缓冲区的数据输出到客户端浏览器
            out.write(buffer, 0, len);
        }
        inStream.close();
    }


}